package com.atguigu.flink.utils;

import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Smexy on 2023/6/20
 */
public class MyUtil
{
    //把窗口中的所有数据转为List集合
    public static <T> List<T> parseToList(Iterable<T> iterable){

        List<T> result = new ArrayList<>();

        for (T t : iterable) {
            result.add(t);
        }

        return result;
    }

    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static String parseTimeWindow(TimeWindow w){

        //从Long ts 转为 日期字符串
        String startStr = LocalDateTime.ofInstant(Instant.ofEpochMilli(w.getStart()), ZoneId.of("Asia/Shanghai")).format(formatter);
        String endStr = LocalDateTime.ofInstant(Instant.ofEpochMilli(w.getEnd()), ZoneId.of("Asia/Shanghai")).format(formatter);

        return "["+startStr + "," +endStr+")";

    }
}
